package com.valdemar.cookerybook.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.valdemar.cookerybook.dao.IngredientDao;
import com.valdemar.cookerybook.model.Ingredient;

public class IngredientResource {

	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String code;
	int languageId;

	public IngredientResource(UriInfo uriInfo, Request request, String code, int languageId) {
		this.uriInfo = uriInfo;
		this.request = request;
		this.code = code;
		this.languageId = languageId;
	}

	// Application integration
	@GET
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Ingredient getIngredient() {
		Ingredient ingredient = IngredientDao.instance.getIngredient(code, languageId);
		if (ingredient == null)
			throw new RuntimeException("Get: Ingredient with code " + code + " not found");
		return ingredient;
	}

//	// For the browser
//	@GET
//	@Produces(MediaType.TEXT_XML)
//	public Ingredient getIngredientHTML() {
//		Ingredient ingredient = IngredientDAO.instance.getIngredient(Long.parseLong(id));
//		if (ingredient == null)
//			throw new RuntimeException("Get: Todo with " + id + " not found");
//		return ingredient;
//	}

//	@PUT
//	@Consumes(MediaType.APPLICATION_XML)
//	public Response putTodo(JAXBElement<Ingredient> todo) {
//		Ingredient c = todo.getValue();
//		return putAndGetResponse(c);
//	}

//	@DELETE
//	public void deleteTodo() {
//		boolean deleted = IngredientDAO.instance.remove(Long.parseLong(id));
//		if (!deleted)
//			throw new RuntimeException("Delete: Ingredient with " + id + " not found");
//	}

//	private Response putAndGetResponse(Todo todo) {
//		Response res;
//		if (TodoDao.instance.getModel().containsKey(todo.getId())) {
//			res = Response.noContent().build();
//		} else {
//			res = Response.created(uriInfo.getAbsolutePath()).build();
//		}
//		TodoDao.instance.getModel().put(todo.getId(), todo);
//		return res;
//	}

}
